﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySqlConnector;
using System.Data;

namespace ClassMySQL.Classes
{
    public static class ServerMySQL
    {
        // Поля для соединения с БД (private)
        private static string _hostName = "localhost";
        private static string _dbName = "mysql";
        private static string _login = "root";
        private static string _pass = "";

        // Поля для создания строки подключения
        private static MySqlConnectionStringBuilder db;
        private static MySqlConnection connection;
        private static MySqlCommand cmd;
        private static string sql;

        // Создание строки подключения
        /// <summary>
        /// Логин: root
        /// Пароль: ваш пароль
        /// </summary>
        /// <param name="pass"></param>
        public static void DB_Load(string pass)
        {
            db = new MySqlConnectionStringBuilder
            {
                Server = _hostName,
                Database = _dbName,
                UserID = _login,
                Password = pass,
                CharacterSet = "utf8"
            };
            connection = new MySqlConnection(db.ConnectionString);
        }

        /// <summary>
        /// Сервер: localhost
        /// Укажите название БД.
        /// Логин: ваш логин
        /// Пароль: ваш пароль 
        /// </summary>
        /// <param name="login"></param>
        /// <param name="pass"></param>
        /// <param name="dbName"></param>
        public static void DB_Load(string dbName, string login, string pass)
        {
            db = new MySqlConnectionStringBuilder
            {
                Server = _hostName,
                Database = dbName,
                UserID = login,
                Password = pass,
                CharacterSet = "utf8"
            };
            connection = new MySqlConnection(db.ConnectionString);
        }

        /// <summary>
        /// Укажите адрес сервера.
        /// Укажите название базы данных.
        /// Укажите логин и пароль.
        /// </summary>
        /// <param name="host"></param>
        /// <param name="dbName"></param>
        /// <param name="login"></param>
        /// <param name="pass"></param>
        public static void DB_Load(string host, string dbName, string login, string pass)
        {
            db = new MySqlConnectionStringBuilder
            {
                Server = host,
                Database = dbName,
                UserID = login,
                Password = pass,
                CharacterSet = "utf8"
            };
            connection = new MySqlConnection(db.ConnectionString);
        }

        
        /// <summary>
        /// Проверка соединения с БД.
        /// True - соединение установлено.
        /// </summary>
        /// <returns></returns>
        public static bool CheckConnect()
        {
            try
            {
                connection.Open();
                connection.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Получение данных с сервера
        public static DataTable GetDataTable(string tableName)
        {
            DataTable dt = new DataTable();
            sql = $"SELECT * FROM {db.Database}.{tableName}";
            cmd = new MySqlCommand(sql, connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            adapter.Fill(dt);
            return dt;
        }
    }
}
